// pages/orderSure/orderSure.js
var httpUtil = require('../../utils/httpUtil.js');
var util = require('../../utils/util.js');

var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goods:{},
    money:0.0,
    goodsTravelSpecification:{},
    date:'',
    year:'',
    month: '',
    coupon:'',
    addMoneyImg:'',
    couponNumber:'',
    addCouponImg:'',
    addMoney:0,
    addCoupon:'../images/shop-yes.png',
    user: { name: '', pinYinName:'', tel: '', eMail: '', spareTel:'',wx:''},
    ifTicket:false,
    ticketNum: {},
    code:'',
    text:{},
    text1:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    var pages = getCurrentPages();

    var prevPage = pages[pages.length - 2];  //上一个页面
     this.setData({
      goods: prevPage.data.goods,
      goodsTravelSpecification: prevPage.data.goodsTravelSpecificationList[prevPage.data.currIndex],
        money: prevPage.data.money,
       date: prevPage.data.year + "-" + prevPage.data.month + "-" + prevPage.data.getDate,
       ticketNum: prevPage.data.ticketNum
    })
    httpUtil.getHttp("/search/get/text"
      , { id: 8 }, function (res) {
        //将markdown内容转换为towxml数据
        let data = app.towxml.toJson(res.text, 'markdown');
        that.setData({
          text1: data
        })
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  goChuxing:function(){
    wx.navigateTo({
      url: '../goOut/goOut'
    })
  },
  goAdditional:function(){
    wx.navigateTo({
      url: '../additional/additional'
    })
  },
  lookMore:function(){
    wx.navigateTo({
      url: '../answer/answer'
    })
  },
  selContact:function(){
    wx.addPhoneContact({
      success:function(e) {
        console.log(e);
      }
    })
  },
  codeSend:function(e){
    //获取列表
      var that = this;

    if (that.data.code==''){
      wx.showToast({
        title: '请输入优惠码',
        icon: 'loading',
        duration: 1000,
        mask: true
      })
      return;
    }
    httpUtil.getHttp("/coupon/get/" + that.data.code+"/code", {}, function (coupon) {
      if (coupon == null || coupon==''){
        wx.showToast({
          title: '优惠卷不存在',
          icon: 'loading',
          duration: 1000,
          mask: true
        })
        return;
      } if (coupon.status == 0) {
        wx.showToast({
          title: '优惠卷未领取',
          icon: 'loading',
          duration: 1000,
          mask: true
        })
        return;
      } if (coupon.status == 2) {
        wx.showToast({
          title: '优惠卷已使用',
          icon: 'loading',
          duration: 1000,
          mask: true
        })
        return;
      }
      if (coupon.startStatus==1) {
        wx.showToast({
          title: '优惠卷未生效',
          icon: 'loading',
          duration: 1000,
          mask: true
        })
        return;
      } 
      if (that.data.money<coupon.total) {
        wx.showToast({
          title: '未达到满减条件',
          icon: 'loading',
          duration: 1000,
          mask: true
        })
        return;
      }
      console.log(parseFloat(that.data.money));
      console.log(coupon.num);

      var money = parseFloat(that.data.money) - coupon.swcjCouponType.num <= 0 ? 0.01 : parseFloat(that.data.money) - coupon.swcjCouponType.num
      if (money == null || money == NaN || money =="NaN"){
        return;
      }
      console.log(money);
      console.log(money);
        that.setData({
          coupon: coupon,
          money: money
        })
      })
    
  },
  setName:function(e){
      var that=this;
    that.data.user.name = e.detail.value;
  },
  setPinYinName: function (e) {
    var that = this;
    that.data.user.pinYinName = e.detail.value;


  }, setTel: function (e) {
    var that = this;
    that.data.user.tel = e.detail.value;

  }, setEmail: function (e) {
    var that = this;
    that.data.user.eMail = e.detail.value;

  }, setSpareTel: function (e) {
    var that = this;
    that.data.user.spareTel = e.detail.value;

  }, setWx: function (e) {
    var that = this;
    that.data.user.wx = e.detail.value;
  }, 
  setCode: function (e) {
    var that = this;
    that.data.code = e.detail.value;
  },
  addMoney:function(){
    var that=this;
    console.log(that.data.addMoneyImg );

    console.log(that.data.addMoneyImg == '' ? '../images/shop-yes.png' : '');
    this.setData({
      addMoneyImg: that.data.addMoneyImg == '' ?'../images/shop-yes.png':'',
      addMoney: that.data.addMoneyImg == '' ? 56 :0,
      money: that.data.addMoneyImg == '' ? parseFloat(parseFloat(that.data.money) + 56).toFixed(2) : parseFloat( parseFloat(that.data.money)-56).toFixed(2)
    })
  },
  addCouponImg:function(){
    var that = this;
    this.setData({
      addCouponImg: '../images/shop-yes.png'
    })
    this.setData({
      addCoupon: '',
    })
  },
  addCoupon: function () {
    var that = this;
    console.log("进入");
    this.setData({
      addCoupon: '../images/shop-yes.png'
    })
    this.setData({
      addCouponImg: '',
    })
    console.log(that.data.addCouponImg);
    console.log(that.data.addCoupon);

  },
  paySuccess: function () {
    var that = this;
    if (that.data.ifTicket == false) {
      wx.showToast({
        title: '请填写出行人',
        icon: 'loading',
        duration: 1000,
        mask: true
      })
      return;
    }
    var user = that.data.user;
    if (user.name == '') {
      wx.showToast({
        title: '请填写姓名',
        icon: 'loading',
        duration: 1000,
        mask: true
      })
      return;
    }
    if (user.name.length<2) {
      wx.showToast({
        title: '请填写真实姓名',
        icon: 'loading',
        duration: 1000,
        mask: true
      })
      return;
    }
    if (user.pinYinName == '') {
      wx.showToast({
        title: '请填写姓名拼音',
        icon: 'loading',
        duration: 1000,
        mask: true
      })
      return;
    }
    if (user.pinYinName.length<=3) {
      wx.showToast({
        title: '拼音不正常',
        icon: 'loading',
        duration: 1000,
        mask: true
      })
      return;
    }
    if (user.eMail == '') {
      wx.showToast({
        title: '请填写邮箱',
        icon: 'loading',
        duration: 1000,
        mask: true
      })
      return;
    }
    var myreg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
    if (!myreg.test(user.eMail ))  {
      wx.showToast({
        title: '无效的的eMail',
        icon: 'loading',
        duration: 1000,
        mask: true
      })
      return;
    }

    if (user.tel == '') {
      wx.showToast({
        title: '请填写手机号',
        icon: 'loading',
        duration: 1000,
        mask: true
      })
      return;
    }
    var myregTel = /^[1][2,3,4,6,5,7,8][0-9]{9}$/;  
    if (!myregTel.test(user.tel)) {
      wx.showToast({
        title: '无效的手机号',
        icon: 'loading',
        duration: 1000,
        mask: true
      })
      return;
    }
    var userInfo = util.getKey("userInfo");
    var travelMoney=0;
    if (that.data.addMoneyImg!=''){
      travelMoney=56;
    }
    var code = that.data.coupon.code;
    if(code==undefined){
      code="";
    }
    console.log( that.data.ticketNum);
    var data = { name: user.name, pinYinName: user.pinYinName, eMail: user.eMail, tel: user.tel, spareTel: user.spareTel, wx: user.wx, list:that.data.ticketNum}
    httpUtil.postHttp("/order/pay/travel?userId=" + userInfo.id + "&goodsId=" + that.data.goods.id + "&specificationId=" + that.data.goodsTravelSpecification.id + "&couponCode=" + code + "&travelMoney=" + travelMoney + "&date=" + that.data.date
      , data, function (res) {
        wx.requestPayment({
          'timeStamp': res.data.timeStamp,
          'nonceStr': res.data.nonceStr,
          'package': res.data.package,
          'signType': res.data.signType,
          'paySign': res.data.paySign,
          'success': function (res) {
            wx.navigateTo({
              url: '../paySuccess/paySuccess'
            })
          },
          'fail': function (res) {
            console.log(res);
            wx.showToast({
              title: "支付失败",
              icon: 'loading',
              duration: 500
            });

          }
        })
      })
  },
})